package com.LC.Service.Impl;

import com.LC.Constant.MessageConstant;
import com.LC.DAO.MemberDAO;
import com.LC.DAO.OrderDAO;
import com.LC.DAO.OrderSettingDAO;
import com.LC.DAO.SetMealDAO;
import com.LC.Entity.Order;
import com.LC.Entity.PageResult;
import com.LC.Entity.QueryPageBean;
import com.LC.Entity.Result;
import com.LC.Service.OrderService;
import com.LC.Utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDAO orderDAO;
    
    @Autowired
    MemberDAO memberDAO;
    
    @Autowired
    SetMealDAO setMealDAO;
    
    @Autowired
    OrderSettingDAO orderSettingDAO;
    
    @Override
    public Result NewOrder(Map map) throws Exception {
        //1.检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有则无法进行预约
        if (orderDAO.findOrderDate(map.get("orderDate").toString()) == 0){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.检查用户所选日期是否已经约满，如果约满则无法预约
        if (orderDAO.findReservation((map.get("orderDate").toString())) == 
            orderDAO.findNumber(map.get("orderDate").toString())){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //3.检查当前用户是否为会员，如果不是则完成注册
        if (memberDAO.findMemberCount((map.get("telephone").toString())) == 0){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            map.put("regTime",simpleDateFormat.format(new Date()));
            String s = map.get("regTime").toString().substring(0,4) + map.get("regTime").toString().substring(5,7) + 
                        map.get("regTime").toString().substring(8,10);
            
            int init = 0;
            String fileNumber = s;
            if (memberDAO.findClearFileNumberCount(s + "001") == 0){
                fileNumber = s + "001";
            }else {
                if (memberDAO.findFileNumberCount(s)<=8){
                    fileNumber = s + "00" + (memberDAO.findFileNumberCount(s) +1);
                }
                if (memberDAO.findFileNumberCount(s)>8 && memberDAO.findFileNumberCount(s)<=98){
                    fileNumber = s + "0" + (memberDAO.findFileNumberCount(s) +1);
                }
                if (memberDAO.findFileNumberCount(s)>98){
                    fileNumber = s + (memberDAO.findFileNumberCount(s) +1);
                }
            }
            map.put("fileNumber",fileNumber);
            String year = map.get("idCard").toString().substring(6, 10);
            String month = map.get("idCard").toString().substring(10, 12);
            String day = map.get("idCard").toString().substring(12, 14);
            Date birthday = DateUtils.parseString2Date(year + "-" + month + "-" + day, "yyyy-MM-dd");
            map.put("birthday",birthday);
            memberDAO.addMember(map);
        }
        //4.检查用户是否重复预约(同一个用户在同一天预约了同一个套餐)，如果是重复预约则无法再次预约
        if (orderDAO.findRepeatOrderCount(memberDAO.findMemberIdByTelephone((map.get("telephone").toString())),
            map.get("orderDate").toString(),Integer.valueOf(map.get("setmealId").toString())) != 0){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        //5.进行预约，预约成功后更新当日已预约人数
        map.put("member_id",memberDAO.findMemberIdByTelephone((map.get("telephone").toString())));
        map.put("orderStatus","未到诊");
        map.put("memberName",memberDAO.findMemberById(Integer.valueOf(map.get("member_id").toString())).getName());
        map.put("gender",memberDAO.findMemberById(Integer.valueOf(map.get("member_id").toString())).getSex());
        map.put("fileNumber",memberDAO.findMemberById(Integer.valueOf(map.get("member_id").toString())).getFileNumber());
        map.put("setmealName",setMealDAO.findById(Integer.valueOf(map.get("setmealId").toString())).getName());
        orderSettingDAO.upDateReservations(orderDAO.findReservation((map.get("orderDate").toString()))+1,(map.get("orderDate").toString()));
        orderDAO.AddOrder(map);
        int orderId = orderDAO.findOrderIdByCondition(map);
        return new Result(true,MessageConstant.ORDER_SUCCESS,orderId);
    }
    
    @Override
    public Map OrderInfoById(int id) throws Exception {
        Order orderById = orderDAO.findOrderById(id);
        Map map = new HashMap();
        map.put("member",memberDAO.findMemberById(orderById.getMemberId()).getName());
        map.put("setmeal",setMealDAO.findById(orderById.getSetmealId()).getName());
        map.put("orderDate",DateUtils.parseDate2String(orderById.getOrderDate(),"yyyy-MM-dd"));
        map.put("orderType",orderById.getOrderType());
        return map;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Order> page = orderDAO.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

   /*
    @Override
    public void AddBKOrder(Order order) {
        try {
            orderDAO.AddBKOrder(order);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    @Override
    public Order GetOrderById(int id) {
        return orderDAO.findOrderById(id);
    }

}
